/*
 * Project: SmartWard
 * Package: com.ruoyi.ichen.app.service
 * File: GoodsService
 * Date: 2020-2-13 14:21
 * Version: V1.0
 * Copyright © 2018 - 2019 YESOFT CHINA, Inc. All Rights Reserved.
 */
package com.ruoyi.ichen.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ichen.app.enums.AppStatusEnum;
import com.ruoyi.ichen.app.mapper.GoodsMapper;
import com.ruoyi.ichen.base.service.BaseDBService;
import com.ruoyi.ichen.entity.IGoods;
import com.ruoyi.ichen.entity.dto.GoodsSearchDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: GoodsService
 * @Description: TODO(用一句话描述该类)
 * @Author chenhuanghua
 * @Date 2020-2-13 14:21
 * @Version V1.0
 */

@Service
public class GoodsService extends BaseDBService<IGoods> {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 商品搜索
     * @param dto
     */
    public List<IGoods> findGoodsReport(GoodsSearchDto dto){
        //
        if(StringUtils.isNotEmpty(dto.getDayStartTime())){
            //日时间处理
            if(StringUtils.isNotEmpty(dto.getDayEndTime())){
                dto.setDayEndTime(this.joinEndTime(dto.getDayEndTime()));
            }else{
                dto.setDayEndTime(this.joinEndTime(dto.getDayStartTime()));
            }
            dto.setDayStartTime(this.joinStartTime(dto.getDayStartTime()));
            //月份时间处理
            if(StringUtils.isNotEmpty(dto.getMonEndTime()) && StringUtils.isEmpty(dto.getMonEndTime())){
                dto.setMonEndTime(dto.getMonStartTime());
            }
        }
        List<IGoods> list = goodsMapper.findGoodsReport(dto);
        if(list != null && list.size() > 0){
            for(IGoods goods : list){
                BigDecimal multiply = BigDecimal.valueOf(goods.getPrice()).multiply(BigDecimal.valueOf(goods.getQuantity()));
                goods.setAmount(multiply.doubleValue());
            }
        }
        return list;
    }



    /**
     * 商品添加
     * @param goods
     * @return
     */
    public Integer save(IGoods goods){
        if(goods.getId() == null) {//添加
            goods.setCreateBy("admin");
            goods.setCreateTime(new Date());
            goods.setUpdateBy("admin");
            goods.setUpdateTime(new Date());
            goods.setStatus(AppStatusEnum.NORMAL.getValue());
//        goodsMapper.insert(goods);
            return this.insert(goods);
        }else{//更新
            goods.setUpdateBy("admin");
            goods.setUpdateTime(new Date());
            return this.updateByIDSelective(goods);
        }
    }


    private String joinStartTime(String startTime){
        return startTime + " 00:00:00";
    }

    private String joinEndTime(String endTime){
        return endTime + " 23:59:59";
    }
}